package cn.nebula.music.service;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.nebula.music.dao.AlbumDAO;
import cn.nebula.music.dao.CombinationDAO;
import cn.nebula.music.dao.MvDAO;
import cn.nebula.music.dao.PictureDAO;
import cn.nebula.music.dao.SingerDAO;
import cn.nebula.music.dao.UserDAO;
import cn.nebula.music.domain.Album;
import cn.nebula.music.domain.Combination;
import cn.nebula.music.domain.Mv;
import cn.nebula.music.domain.Picture;
import cn.nebula.music.domain.Singer;
import cn.nebula.music.domain.User;

@Service
public class PictureServiceImpl implements PictureService {

	@Autowired
	private PictureDAO pictureDAO;

	@Autowired
	private AlbumDAO albumDAO;

	@Autowired
	private CombinationDAO combinationDAO;

	@Autowired
	private MvDAO mvDAO;

	@Autowired
	private SingerDAO singerDAO;

	@Autowired
	private UserDAO userDAO;

	@Override
	public Set<Picture> findAllPictures() throws Exception {
		return pictureDAO.findAllPictures();
	}

	@Override
	public Set<Picture> findAllPicturesOfBT() throws Exception {
		return pictureDAO.findAllPicturesOfBT();
	}

	@Override
	public Picture findPictureById(Integer id) throws Exception {
		return pictureDAO.findPictureById(id);
	}

	@Override
	public Picture findPictureByResourcepath(String resourcepath) throws Exception {
		return pictureDAO.findPictureByResourcepath(resourcepath);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void addPicture(Picture picture) throws Exception {
		pictureDAO.addPicture(picture);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updatePicture(Picture picture) throws Exception {
		pictureDAO.updatePicture(picture);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deletePictureById(Integer id) throws Exception {
		for (Album album : albumDAO.findAllAlbumsWP()) {
			if (album.getPicture() != null && album.getPicture().getId().equals(id)) {
				album.setPicture(null);
				albumDAO.updateAlbum(album);
			}
		}
		for (Combination combination : combinationDAO.findAllCombinationsWP()) {
			if (combination.getPicture() != null && combination.getPicture().getId().equals(id)) {
				combination.setPicture(null);
				combinationDAO.updateCombination(combination);
			}
		}
		for (Mv mv : mvDAO.findAllMvsWP()) {
			if (mv.getPicture() != null && mv.getPicture().getId().equals(id)) {
				mv.setPicture(null);
				mvDAO.updateMv(mv);
			}
		}
		for (Singer singer : singerDAO.findAllSingersWP()) {
			if (singer.getPicture() != null && singer.getPicture().getId().equals(id)) {
				singer.setPicture(null);
				singerDAO.updateSinger(singer);
			}
		}
		for (User user : userDAO.findAllUsersWP()) {
			if (user.getPicture() != null && user.getPicture().getId().equals(id)) {
				user.setPicture(null);
				userDAO.updateUser(user);
			}
		}
		pictureDAO.deletePictureById(id);

	}

}
